package com.mathdroid.gui.main;

import java.util.LinkedList;

import javax.swing.AbstractListModel;
import javax.swing.MutableComboBoxModel;

/**
 * This ComboBoxModel manages the objects of a JComboBox.
 */
public class DroidComboBoxModel 
extends AbstractListModel 
implements MutableComboBoxModel {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The list of objects.
	 */
	private LinkedList<Object> list = new LinkedList<Object>();
	
	/**
	 * The currently selected object in the comboBox.
	 */
	private Object selectedItem = null;
	
	@Override
	public final void addElement(final Object o) {
		this.list.add(o);
		this.fireContentsChanged(this, 0, this.getSize() - 1);
	}
	
	@Override
	public final Object getElementAt(final int i) {
		return this.list.get(i);
	}

	@Override
	public final int getSize() {
		return this.list.size();
	}

	@Override
	public final Object getSelectedItem() {
		return this.selectedItem;
	}

	@Override
	public final void setSelectedItem(final Object o) {
		this.selectedItem = o;
	}

	@Override
	public final void insertElementAt(final Object o, final int i) {
		this.list.add(i, o);
		this.fireContentsChanged(this, 0, this.getSize() - 1);
	}

	@Override
	public final void removeElement(final Object o) {
		this.list.remove(o);
		this.fireContentsChanged(this, 0, this.getSize() - 1);
	}

	@Override
	public final void removeElementAt(final int i) {
		this.list.remove(i);
		this.fireContentsChanged(this, 0, this.getSize() - 1);
	}
}
